#include "formdcsfileselector.h"
#include "ui_formdcsfileselector.h"
#include "dcsplayer/dcsfileoperator.h"

#include <QDir>
#include <QDebug>
#include <global.h>

FormDCSFileSelector::FormDCSFileSelector(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FormDCSFileSelector)
{
    QString path("");
    quint8 src;

    ui->setupUi(this);
    setWindowFlags(windowFlags() | Qt::Dialog);
    setWindowModality(Qt::ApplicationModal);
    setAttribute(Qt::WA_DeleteOnClose);

    mDbopt = new DCSDbOperator();
    if (mDbopt->getMasterParameterMainVideoSrc(src))
    {
        if (src == VIDEOSRC_EN_UDISK)
        {
            path.append(UDISK_PATH);
        }
        else if (src == VIDEOSRC_EN_SDCARD)
        {
            path.append(SDCARD_PATH);
        }
        else
        {
            path.append(EMMC_PATH);
        }

        qDebug() << "path:" << path;

        QDir d(path);
        QStringList filter;

        filter << "*.sd";
        d.setNameFilters(filter);
        d.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
        d.setSorting(QDir::Name);
        const QFileInfoList list = d.entryInfoList();
        QFileInfoList::const_iterator iterator;

        for (iterator = list.begin(); iterator != list.end(); iterator++)
        {
            QListWidgetItem *item = new QListWidgetItem();
            item->setText(iterator->fileName());
            item->setCheckState(Qt::Unchecked);
            ui->listWidget->addItem(item);
        }

        switch (src)
        {
            case VIDEOSRC_EN_EMMC:
                ui->labelVideoSrc->setText(tr("内部存储"));
                break;
            case VIDEOSRC_EN_SDCARD:
                ui->labelVideoSrc->setText(tr("SD卡"));
                break;
            case VIDEOSRC_EN_UDISK:
                ui->labelVideoSrc->setText(tr("U盘"));
                break;
            case VIDEOSRC_EN_NULL:
            default:
                ui->labelVideoSrc->setText(tr("无"));
                break;
        }

    }


    qDebug() << "[FormDCSFileSelector] \t Constructor";
}

FormDCSFileSelector::~FormDCSFileSelector()
{
    for (int i = 0; i < ui->listWidget->count(); i++)
    {
        delete ui->listWidget->item(i);
    }

    delete ui;
    delete mDbopt;
    qDebug() << "[FormDCSFileSelector] \t Destructor";
}

void FormDCSFileSelector::on_pushButtonSelectAll_clicked()
{
    for (int i = 0; i < ui->listWidget->count(); i++)
    {
        ui->listWidget->item(i)->setCheckState(Qt::Checked);
    }
}

void FormDCSFileSelector::on_pushButtonInvert_clicked()
{
    for (int i = 0; i < ui->listWidget->count(); i++)
    {
        if (ui->listWidget->item(i)->checkState() == Qt::Checked)
        {
            ui->listWidget->item(i)->setCheckState(Qt::Unchecked);
        }
        else
        {
            ui->listWidget->item(i)->setCheckState(Qt::Checked);
        }
    }
}

void FormDCSFileSelector::on_pushButtonOK_clicked()
{
    quint8 index;
    QString abspath;
    struct playListsItem item;

    mDbopt->getMasterParameterMainVideoSrc(index);
    if (index == VIDEOSRC_EN_UDISK)
    {
        abspath.append(UDISK_PATH);
    }
    else if (index == VIDEOSRC_EN_SDCARD)
    {
        abspath.append(SDCARD_PATH);
    }
    else
    {
        abspath.append(EMMC_PATH);
    }

    mDbopt->clearPlayLists();

    for (int i = 0; i < ui->listWidget->count(); i++)
    {
        if (ui->listWidget->item(i)->checkState() == Qt::Checked)
        {
            item.path = abspath + ui->listWidget->item(i)->text();
            qDebug() << item.path;
            mDbopt->addPlayItem(item);
        }
    }

    emit winClose();
    close();
}

void FormDCSFileSelector::on_pushButtonCancel_clicked()
{
    emit winClose();
    close();
}
